<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>MessageBox 弹框 | MyWeb 4.x</title>
    <meta name="generator" content="VuePress 1.5.0">
    <link rel="icon" href="/my/favicon.ico">
    <script>
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?c4e5f73318b5cb0c389e3d9a05f831cc";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
    <meta name="description" content="基于Vue + ElementUI 的web项目工程框架，专注于公检法行业中台系统快速搭建，框架已在多个项目实战检验。">
    <link rel="preload" href="/my/assets/css/0.styles.d7ae9199.css" as="style"><link rel="preload" href="/my/assets/js/app.2b2365a7.js" as="script"><link rel="preload" href="/my/assets/js/535.c100cd07.js" as="script"><link rel="preload" href="/my/assets/js/1284.cef18e9e.js" as="script"><link rel="preload" href="/my/assets/js/1025.8afc0690.js" as="script">
    <link rel="stylesheet" href="/my/assets/css/0.styles.d7ae9199.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/my/" class="home-link router-link-active"><img src="/my/img/logo.png" alt="MyWeb 4.x" class="logo"> <span class="site-name can-hide">MyWeb 4.x</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/my/guide/" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="/my/ui/components/" class="nav-link">
  组件
</a></div><div class="nav-item"><a href="/my/ui/charts/" class="nav-link">
  图表
</a></div><div class="nav-item"><a href="/my/ui/map/" class="nav-link">
  地图
</a></div><div class="nav-item"><a href="/my/ui/go/" class="nav-link">
  关系图
</a></div><div class="nav-item"><a href="/my/ui/icon/" class="nav-link">
  图标
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="JSDoc" class="dropdown-title"><span class="title">JSDoc</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/my/api/components.html" class="nav-link">
  基础组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/utils.html" class="nav-link">
  工具函数
</a></li><li class="dropdown-item"><!----> <a href="/my/api/charts.html" class="nav-link">
  图表组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/map.html" class="nav-link">
  地图组件
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/my/guide/" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="/my/ui/components/" class="nav-link">
  组件
</a></div><div class="nav-item"><a href="/my/ui/charts/" class="nav-link">
  图表
</a></div><div class="nav-item"><a href="/my/ui/map/" class="nav-link">
  地图
</a></div><div class="nav-item"><a href="/my/ui/go/" class="nav-link">
  关系图
</a></div><div class="nav-item"><a href="/my/ui/icon/" class="nav-link">
  图标
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="JSDoc" class="dropdown-title"><span class="title">JSDoc</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/my/api/components.html" class="nav-link">
  基础组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/utils.html" class="nav-link">
  工具函数
</a></li><li class="dropdown-item"><!----> <a href="/my/api/charts.html" class="nav-link">
  图表组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/map.html" class="nav-link">
  地图组件
</a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Basic</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Form</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Data</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Notice</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/my/element/alert.html" class="sidebar-link">Alert 警告</a></li><li><a href="/my/element/loading.html" class="sidebar-link">Loading 加载</a></li><li><a href="/my/element/message.html" class="sidebar-link">Message 消息提示</a></li><li><a href="/my/element/message-box.html" aria-current="page" class="active sidebar-link">MessageBox 弹框</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/my/element/message-box.html#消息提示" class="sidebar-link">消息提示</a></li><li class="sidebar-sub-header"><a href="/my/element/message-box.html#确认消息" class="sidebar-link">确认消息</a></li><li class="sidebar-sub-header"><a href="/my/element/message-box.html#提交内容" class="sidebar-link">提交内容</a></li><li class="sidebar-sub-header"><a href="/my/element/message-box.html#自定义" class="sidebar-link">自定义</a></li><li class="sidebar-sub-header"><a href="/my/element/message-box.html#使用-html-片段" class="sidebar-link">使用 HTML 片段</a></li><li class="sidebar-sub-header"><a href="/my/element/message-box.html#区分取消与关闭" class="sidebar-link">区分取消与关闭</a></li><li class="sidebar-sub-header"><a href="/my/element/message-box.html#居中布局" class="sidebar-link">居中布局</a></li><li class="sidebar-sub-header"><a href="/my/element/message-box.html#全局方法" class="sidebar-link">全局方法</a></li><li class="sidebar-sub-header"><a href="/my/element/message-box.html#单独引用" class="sidebar-link">单独引用</a></li><li class="sidebar-sub-header"><a href="/my/element/message-box.html#options" class="sidebar-link">Options</a></li></ul></li><li><a href="/my/element/notification.html" class="sidebar-link">Notification 通知</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Navigation</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Others</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="messagebox-弹框"><a href="#messagebox-弹框" class="header-anchor">#</a> MessageBox 弹框</h1> <p>模拟系统的消息提示框而实现的一套模态对话框组件，用于消息提示、确认消息和提交内容。</p> <div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>从场景上说，MessageBox 的作用是美化系统自带的 <code>alert</code>、<code>confirm</code> 和 <code>prompt</code>，因此适合展示较为简单的内容。如果需要弹出较为复杂的内容，请使用 Dialog。</p></div> <h2 id="消息提示"><a href="#消息提示" class="header-anchor">#</a> 消息提示</h2> <p>当用户进行操作时会被触发，该对话框中断用户操作，直到用户确认知晓后才可关闭。</p> <!----><h2 id="确认消息"><a href="#确认消息" class="header-anchor">#</a> 确认消息</h2> <p>提示用户确认其已经触发的动作，并询问是否进行此操作时会用到此对话框。</p> <!----><h2 id="提交内容"><a href="#提交内容" class="header-anchor">#</a> 提交内容</h2> <p>当用户进行操作时会被触发，中断用户操作，提示用户进行输入的对话框。</p> <!----><h2 id="自定义"><a href="#自定义" class="header-anchor">#</a> 自定义</h2> <p>可自定义配置不同内容。</p> <!----><div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>弹出层的内容可以是 <code>VNode</code>，所以我们能把一些自定义组件传入其中。每次弹出层打开后，Vue 会对新老 <code>VNode</code> 节点进行比对，然后将根据比较结果进行最小单位地修改视图。这也许会造成弹出层内容区域的组件没有重新渲染，例如 <a href="https://github.com/ElemeFE/element/issues/8931" target="_blank" rel="noopener noreferrer">#8931<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>。当这类问题出现时，解决方案是给 <code>VNode</code> 加上一个不相同的 key，参考<a href="https://jsfiddle.net/zhiyang/ezmhq2ef/" target="_blank" rel="noopener noreferrer">这里<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>。</p></div> <h2 id="使用-html-片段"><a href="#使用-html-片段" class="header-anchor">#</a> 使用 HTML 片段</h2> <p><code>message</code> 属性支持传入 HTML 片段。</p> <!----><div class="custom-block warning"><p class="custom-block-title">WARNING</p> <p><code>message</code> 属性虽然支持传入 HTML 片段，但是在网站上动态渲染任意 HTML 是非常危险的，因为容易导致 <a href="https://en.wikipedia.org/wiki/Cross-site_scripting" target="_blank" rel="noopener noreferrer">XSS 攻击<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>。因此在 <code>dangerouslyUseHTMLString</code> 打开的情况下，请确保 <code>message</code> 的内容是可信的，<strong>永远不要</strong>将用户提交的内容赋值给 <code>message</code> 属性。</p></div> <h2 id="区分取消与关闭"><a href="#区分取消与关闭" class="header-anchor">#</a> 区分取消与关闭</h2> <p>有些场景下，点击取消按钮与点击关闭按钮有着不同的含义。</p> <!----><h2 id="居中布局"><a href="#居中布局" class="header-anchor">#</a> 居中布局</h2> <p>内容支持居中布局</p> <!----><h2 id="全局方法"><a href="#全局方法" class="header-anchor">#</a> 全局方法</h2> <p>如果你完整引入了 Element，它会为 Vue.prototype 添加如下全局方法：$msgbox, $alert, $confirm 和 $prompt。因此在 Vue instance 中可以采用本页面中的方式调用 <code>MessageBox</code>。调用参数为：</p> <ul><li><code>$msgbox(options)</code></li> <li><code>$alert(message, title, options)</code> 或 <code>$alert(message, options)</code></li> <li><code>$confirm(message, title, options)</code> 或 <code>$confirm(message, options)</code></li> <li><code>$prompt(message, title, options)</code> 或 <code>$prompt(message, options)</code></li></ul> <h2 id="单独引用"><a href="#单独引用" class="header-anchor">#</a> 单独引用</h2> <p>如果单独引入 <code>MessageBox</code>：</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> MessageBox <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'element-ui'</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>那么对应于上述四个全局方法的调用方法依次为：MessageBox, MessageBox.alert, MessageBox.confirm 和 MessageBox.prompt，调用参数与全局方法相同。</p> <h2 id="options"><a href="#options" class="header-anchor">#</a> Options</h2> <table><thead><tr><th>参数</th> <th>说明</th> <th>类型</th> <th>可选值</th> <th>默认值</th></tr></thead> <tbody><tr><td>title</td> <td>MessageBox 标题</td> <td>string</td> <td>—</td> <td>—</td></tr> <tr><td>message</td> <td>MessageBox 消息正文内容</td> <td>string / VNode</td> <td>—</td> <td>—</td></tr> <tr><td>dangerouslyUseHTMLString</td> <td>是否将 message 属性作为 HTML 片段处理</td> <td>boolean</td> <td>—</td> <td>false</td></tr> <tr><td>type</td> <td>消息类型，用于显示图标</td> <td>string</td> <td>success / info / warning / error</td> <td>—</td></tr> <tr><td>iconClass</td> <td>自定义图标的类名，会覆盖 <code>type</code></td> <td>string</td> <td>—</td> <td>—</td></tr> <tr><td>customClass</td> <td>MessageBox 的自定义类名</td> <td>string</td> <td>—</td> <td>—</td></tr> <tr><td>callback</td> <td>若不使用 Promise，可以使用此参数指定 MessageBox 关闭后的回调</td> <td>function(action, instance)，action 的值为'confirm', 'cancel'或'close', instance 为 MessageBox 实例，可以通过它访问实例上的属性和方法</td> <td>—</td> <td>—</td></tr> <tr><td>showClose</td> <td>MessageBox 是否显示右上角关闭按钮</td> <td>boolean</td> <td>—</td> <td>true</td></tr> <tr><td>beforeClose</td> <td>MessageBox 关闭前的回调，会暂停实例的关闭</td> <td>function(action, instance, done)，action 的值为'confirm', 'cancel'或'close'；instance 为 MessageBox 实例，可以通过它访问实例上的属性和方法；done 用于关闭 MessageBox 实例</td> <td>—</td> <td>—</td></tr> <tr><td>distinguishCancelAndClose</td> <td>是否将取消（点击取消按钮）与关闭（点击关闭按钮或遮罩层、按下 ESC 键）进行区分</td> <td>boolean</td> <td>—</td> <td>false</td></tr> <tr><td>lockScroll</td> <td>是否在 MessageBox 出现时将 body 滚动锁定</td> <td>boolean</td> <td>—</td> <td>true</td></tr> <tr><td>showCancelButton</td> <td>是否显示取消按钮</td> <td>boolean</td> <td>—</td> <td>false（以 confirm 和 prompt 方式调用时为 true）</td></tr> <tr><td>showConfirmButton</td> <td>是否显示确定按钮</td> <td>boolean</td> <td>—</td> <td>true</td></tr> <tr><td>cancelButtonText</td> <td>取消按钮的文本内容</td> <td>string</td> <td>—</td> <td>取消</td></tr> <tr><td>confirmButtonText</td> <td>确定按钮的文本内容</td> <td>string</td> <td>—</td> <td>确定</td></tr> <tr><td>cancelButtonClass</td> <td>取消按钮的自定义类名</td> <td>string</td> <td>—</td> <td>—</td></tr> <tr><td>confirmButtonClass</td> <td>确定按钮的自定义类名</td> <td>string</td> <td>—</td> <td>—</td></tr> <tr><td>closeOnClickModal</td> <td>是否可通过点击遮罩关闭 MessageBox</td> <td>boolean</td> <td>—</td> <td>true（以 alert 方式调用时为 false）</td></tr> <tr><td>closeOnPressEscape</td> <td>是否可通过按下 ESC 键关闭 MessageBox</td> <td>boolean</td> <td>—</td> <td>true（以 alert 方式调用时为 false）</td></tr> <tr><td>closeOnHashChange</td> <td>是否在 hashchange 时关闭 MessageBox</td> <td>boolean</td> <td>—</td> <td>true</td></tr> <tr><td>showInput</td> <td>是否显示输入框</td> <td>boolean</td> <td>—</td> <td>false（以 prompt 方式调用时为 true）</td></tr> <tr><td>inputPlaceholder</td> <td>输入框的占位符</td> <td>string</td> <td>—</td> <td>—</td></tr> <tr><td>inputType</td> <td>输入框的类型</td> <td>string</td> <td>—</td> <td>text</td></tr> <tr><td>inputValue</td> <td>输入框的初始文本</td> <td>string</td> <td>—</td> <td>—</td></tr> <tr><td>inputPattern</td> <td>输入框的校验表达式</td> <td>regexp</td> <td>—</td> <td>—</td></tr> <tr><td>inputValidator</td> <td>输入框的校验函数。可以返回布尔值或字符串，若返回一个字符串, 则返回结果会被赋值给 inputErrorMessage</td> <td>function</td> <td>—</td> <td>—</td></tr> <tr><td>inputErrorMessage</td> <td>校验未通过时的提示文本</td> <td>string</td> <td>—</td> <td>输入的数据不合法!</td></tr> <tr><td>center</td> <td>是否居中布局</td> <td>boolean</td> <td>—</td> <td>false</td></tr> <tr><td>roundButton</td> <td>是否使用圆角按钮</td> <td>boolean</td> <td>—</td> <td>false</td></tr></tbody></table></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/my/element/message.html" class="prev">
        Message 消息提示
      </a></span> <span class="next"><a href="/my/element/notification.html">
        Notification 通知
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/my/assets/js/app.2b2365a7.js" defer></script><script src="/my/assets/js/535.c100cd07.js" defer></script><script src="/my/assets/js/1284.cef18e9e.js" defer></script><script src="/my/assets/js/1025.8afc0690.js" defer></script>
  </body>
</html>
